package com.vnp.vchat.database;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.content.ContentValues;
import android.util.Log;

import com.vnp.vchat.bean.ObjKhachhang;
import com.vnp.vchat.bean.ObjMsgChat;


public class DBFunction {
	private DBAdapter db;
	
	public DBFunction(DBAdapter db) {
		this.db = db;
	}
	
	public void dropDB() {
		try {
			db.delete(MySQLiteHelper.TABLE_CONTACT_PHONE, "1=1");
			db.delete(MySQLiteHelper.TABLE_HISCHAT, "1=1");
			db.delete(MySQLiteHelper.TABLE_KH, "1=1");
			db.delete(MySQLiteHelper.TABLE_MESSAGE, "1=1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// insert phan tu vao chat;
	public void insertMSG(ObjMsgChat objMsgChat, String relation, int status, String msgId){
		try{
			ContentValues value = new ContentValues();
			
			value.put(MySQLiteHelper.MSG_RELATION, relation);
			value.put(MySQLiteHelper.MSG_ID, msgId);
			value.put(MySQLiteHelper.MSG_SEND_ID, Integer.valueOf(objMsgChat.getFromId()));
			value.put(MySQLiteHelper.MSG_TIME, 0);
			value.put(MySQLiteHelper.MSG_TYPE, objMsgChat.getType());
			value.put(MySQLiteHelper.MSG_NAME, objMsgChat.getTenKhachHang());
			value.put(MySQLiteHelper.MSG_CONTENT, objMsgChat.getContent());
			value.put(MySQLiteHelper.MSG_URL_IMAGE, objMsgChat.getUrlImageKH());
			value.put(MySQLiteHelper.MSG_AVATAR_KH, objMsgChat.getUrlAvatarKhachhang());
			value.put(MySQLiteHelper.MSG_STATUS, status);
			
			db.insert(MySQLiteHelper.TABLE_MESSAGE, value);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void updateStatusMsg(String msgId, int status){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.MSG_STATUS, status);
		// update   
//		db.update(TABLE_MESSAGE, values, MSG_ID + " = ?", new String[]{msgId});
		db.update(MySQLiteHelper.TABLE_MESSAGE, values, MySQLiteHelper.MSG_ID + " = "+msgId);
	}
	
	public void updateStatusMySMS(String msgId, int status, long vcg){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.MSG_STATUS, status);
		
		// update 
		db.update(MySQLiteHelper.TABLE_MESSAGE, values, MySQLiteHelper.MSG_ID + " = "+msgId);
	}
	
	public void updateMsg(String url, String key_row){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.MSG_URL_IMAGE, url);
		
		// update 
		db.update(MySQLiteHelper.TABLE_MESSAGE, values, MySQLiteHelper.MSG_SEND_ID + " = "+key_row);
	}
	
	public void deteleAllRecord(String key_row){
		db.delete(MySQLiteHelper.TABLE_MESSAGE, MySQLiteHelper.MSG_RELATION + " = "+ key_row);
	}
	
	public void deleteOneHisChat(String id){
		db.delete(MySQLiteHelper.TABLE_HISCHAT, MySQLiteHelper.HS_UID + " = "+ id);
	}
	
	public ArrayList<String> getIdHis(){
		ArrayList<String> items = new ArrayList<String>();
		String query = "SELECT "+MySQLiteHelper.HS_UID+ " FROM "+ MySQLiteHelper.TABLE_HISCHAT;
		String[] columns = {MySQLiteHelper.HS_UID};
		ArrayList<LinkedHashMap<String, Object>> records = db.fetch(MySQLiteHelper.TABLE_MESSAGE, columns, null, MySQLiteHelper.MSG_TIME + "DESC", 20, 0, null);
		for (int i = 0; i < records.size(); i++) {
			LinkedHashMap<String, Object> row = records.get(i);
			String id = (String) row.get(MySQLiteHelper.HS_UID);
			items.add(id);
		}
		return items;
	}
	
	// get all phan tu
	public ArrayList<ObjMsgChat> getAllMsgHis(String relation){
		ArrayList<ObjMsgChat> objMsgChats = new ArrayList<ObjMsgChat>();
		String selectQuery = "SELECT * FROM " + MySQLiteHelper.TABLE_MESSAGE+ " WHERE "+MySQLiteHelper.MSG_RELATION+ "= "+ relation ;
		Log.i("SQL", selectQuery);
		
		String[] columns = {MySQLiteHelper.MSG_ID, MySQLiteHelper.MSG_SEND_ID, MySQLiteHelper.MSG_TIME,
				MySQLiteHelper.MSG_TYPE, MySQLiteHelper.MSG_NAME, MySQLiteHelper.MSG_CONTENT,
				MySQLiteHelper.MSG_URL_IMAGE, MySQLiteHelper.MSG_AVATAR_KH, MySQLiteHelper.MSG_STATUS};
		
		ArrayList<LinkedHashMap<String, Object>> records = db.fetch(MySQLiteHelper.TABLE_MESSAGE, columns, MySQLiteHelper.MSG_RELATION+ "= "+relation, MySQLiteHelper.MSG_TIME+" ASC", 20, 0, null);
		for (int i = 0; i < records.size(); i++) {
			
			ObjMsgChat objMsgChat = new ObjMsgChat();
			LinkedHashMap<String, Object> row = records.get(i);
			
			objMsgChat.setMsgID((String) row.get(MySQLiteHelper.MSG_ID));
			objMsgChat.setFromId((String) row.get(MySQLiteHelper.MSG_SEND_ID));
			objMsgChat.setTime((Integer) row.get(MySQLiteHelper.MSG_TIME));
			objMsgChat.setType((String) row.get(MySQLiteHelper.MSG_TYPE));
			objMsgChat.setTenKhachHang((String) row.get(MySQLiteHelper.MSG_NAME));
			objMsgChat.setContent((String) row.get(MySQLiteHelper.MSG_CONTENT));
			objMsgChat.setUrlImageKH((String) row.get(MySQLiteHelper.MSG_URL_IMAGE));
			objMsgChat.setUrlAvatarKhachhang((String) row.get(MySQLiteHelper.MSG_AVATAR_KH));
			objMsgChat.setStatus((Integer) row.get(MySQLiteHelper.MSG_STATUS));
			// add vao list
			objMsgChats.add(objMsgChat);
		}
		return objMsgChats;
	}
	
	public void insertContactSV(ObjKhachhang objKhachhang){
		
		ContentValues values = new ContentValues();
		
		values.put(MySQLiteHelper.CT_ID_SV, objKhachhang.getId());
		values.put(MySQLiteHelper.CT_PHONE_ID_SV, objKhachhang.getPhone());
		values.put(MySQLiteHelper.CT_EMAIL_SV, objKhachhang.getEmail());
		values.put(MySQLiteHelper.CT_NAME_SV, objKhachhang.getName());
		values.put(MySQLiteHelper.CT_AVATAR_SV, objKhachhang.getUrlAvatar());
		values.put(MySQLiteHelper.CT_URL_SV, "");
		
		db.insert(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, values);
		
	}
	
	
	public void updateContactSV (ObjKhachhang objKhachhang){
		
		ContentValues values = new ContentValues();
		
		values.put(MySQLiteHelper.CT_ID_SV, objKhachhang.getId());
		values.put(MySQLiteHelper.CT_PHONE_ID_SV, objKhachhang.getPhone());
		values.put(MySQLiteHelper.CT_EMAIL_SV, objKhachhang.getEmail());
		values.put(MySQLiteHelper.CT_NAME_SV, objKhachhang.getName());
		values.put(MySQLiteHelper.CT_AVATAR_SV, objKhachhang.getUrlAvatar());
		values.put(MySQLiteHelper.CT_URL_SV, "");
		
		boolean isExit = db.update(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, values, MySQLiteHelper.CT_PHONE_ID_SV+ " = "+objKhachhang.getPhone());
		
		Log.i("SQL", "Update: "+isExit);
		
		if (isExit == false){
			insertContactSV(objKhachhang);
			Log.i("SQL", "insert-------------------");
		} else {
			Log.i("SQL", "update-------------------");
		}
	}
	
	public ArrayList<ObjKhachhang> getContactSV (){
		ArrayList<ObjKhachhang> objKhachhangs = new ArrayList<ObjKhachhang>();
		String selectQuery = "SELECT * FROM " + MySQLiteHelper.TABLE_CONTACT_PHONE_SV;
		Log.i("SQL", selectQuery);
		
		String[] columns = {MySQLiteHelper.CT_PHONE_ID_SV, MySQLiteHelper.CT_EMAIL_SV,
				MySQLiteHelper.CT_NAME_SV, MySQLiteHelper.CT_AVATAR_SV};
		try {
			ArrayList<LinkedHashMap<String, Object>> records = db.fetchAll(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, null, null);
			
			Log.i("DATA", "RECORD SIZE: "+records.size());
			
				for (int i = 0; i < records.size(); i++) {

					ObjKhachhang objKhachhang = new ObjKhachhang();
					LinkedHashMap<String, Object> row = records.get(i);

					objKhachhang.setId((String) row.get(MySQLiteHelper.CT_ID_SV));
					objKhachhang.setPhone((String) row
							.get(MySQLiteHelper.CT_PHONE_ID_SV));
					objKhachhang.setEmail((String) row
							.get(MySQLiteHelper.CT_EMAIL_SV));
					objKhachhang.setName((String) row
							.get(MySQLiteHelper.CT_NAME_SV));
					objKhachhang.setUrlAvatar((String) row
							.get(MySQLiteHelper.CT_AVATAR_SV));
					objKhachhang.setUrlDangxem("");

					
					objKhachhangs.add(objKhachhang);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		return objKhachhangs;
	}
	
	public void deleteContactSv(){
		db.delete(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, "1=1");
	}
	
}
